/*
 * CostManager.cpp
 *
 *  Created on: 2013-2-3
 *      Author: Administrator
 */

#include "CostManager.h"
#include "ModifyCost.h"

CostManager::CostManager(QWidget *parent):QDialog(parent),
addButton(tr("添加")), modifyButton(tr("修改")), table(100, 4)
{
	qDebug() << "enter CostManager::CostManager";
	QStringList tableHeader;
	tableHeader << "ID" << "类型名称" << "是否启用" << "说明";
	table.setHorizontalHeaderLabels(tableHeader);
	table.setSelectionBehavior(QAbstractItemView::SelectRows);
	table.setSelectionMode(QAbstractItemView::SingleSelection);
	table.setEditTriggers(QAbstractItemView::NoEditTriggers);
	table.setColumnHidden(0, true);
	QHBoxLayout *topLayout = new QHBoxLayout;
	topLayout->addStretch();
	topLayout->addWidget(&addButton);
	topLayout->addWidget(&modifyButton);
	topLayout->addStretch();
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(topLayout);
	mainLayout->addWidget(&table);
	setWindowTitle(tr("管理支出类型"));
	this->setLayout(mainLayout);
//	setFixedHeight(sizeHint().height());
	this->setMinimumSize(370, 200);

	connect(&addButton, SIGNAL(clicked()), this, SLOT(slt_addMode()));
	connect(&modifyButton, SIGNAL(clicked()), this, SLOT(slt_modifyMode()));
	connect(&modifyCost, SIGNAL(sig_modifyOrAdd(const int &, const int &, const QString &, const QString &, const bool &)),
			this, SLOT(addOrModify(const int &, const int &, const QString &, const QString &, const bool &)));
//	connect(&, SIGNAL(), this, SLOT());

	db = Dao.getDateBase();
	fresh();
}

void CostManager::fresh()
{
	QSqlQuery query(db);
	query.exec("SELECT * FROM costtype");
	QSqlRecord record = query.record();
	int idx_id = record.indexOf("id");
	int idx_name = record.indexOf("name");
	int idx_active = record.indexOf("active");
	int idx_comment = record.indexOf("comment");
	int i = 0;
	while(query.next())
	{
		table.setItem(i, 0, new QTableWidgetItem(query.value(idx_id).toString()));
		table.setItem(i, 1, new QTableWidgetItem(query.value(idx_name).toString()));
		QCheckBox *lock = new QCheckBox;
		lock->setChecked(query.value(idx_active).toBool());
		lock->setDisabled(true);
		lock->setText("启用");
		table.setCellWidget(i, 2, lock);
		table.setItem(i, 3, new QTableWidgetItem(query.value(idx_comment).toString()));
		if(++i > table.rowCount())
			table.insertRow(table.rowCount());
	}
}

void CostManager::slt_addMode()
{
	modifyCost.setMode(ModifyCost::addMode);
	modifyCost.show();
}

void CostManager::slt_modifyMode()
{
	int rowCurrent = table.currentRow();
	if(table.item(rowCurrent, 0) == 0)
	{
		qDebug() << "UserManager::slt_modifyMode() return because table.item(table.currentRow(), 0) == ";
		return;
	}
	int idCurrent = table.item(rowCurrent, 0)->text().toInt();
	qDebug() << "table.currentRow() = " << rowCurrent << "; idCurrent = " << idCurrent;
	QSqlQuery query(db);
	query.prepare("SELECT * FROM costtype WHERE id=:ID");
	query.bindValue(":ID", idCurrent);
	query.exec();
	QSqlRecord record = query.record();
	int idx_name = record.indexOf("name");
	int idx_active = record.indexOf("active");
	int idx_comment = record.indexOf("comment");

	if(query.next())
	{
		modifyCost.setMode(ModifyCost::modifyMode, idCurrent, query.value(idx_name).toString(),
				query.value(idx_comment).toString(), query.value(idx_active).toBool());
	}
	modifyCost.show();
}

void CostManager::addOrModify(const int &mode, const int &id,
		const QString &name, const QString &comment, const bool &active)
{
	qDebug() << "enter CostManager::addOrModify; id = " << id;
	QSqlQuery query(db);
	if(mode == ModifyCost::addMode)
	{
		query.prepare("INSERT INTO costtype (name, active, comment)"
				"VALUES(:name, :active, :comment)");
	}
	else
	{
		query.prepare("UPDATE costtype SET name=:name, active=:active, comment=:comment WHERE id=:id");
		query.bindValue(":id", id);
	}
	query.bindValue(":name", name);
	query.bindValue(":active", active);
	query.bindValue(":comment", comment);
	query.exec();
	fresh();
}
CostManager::~CostManager() {
	// TODO Auto-generated destructor stub
}

